#pragma once

namespace sheep
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{}

		// vector(const vector<T>& v)
		// {
		// 	_start = new T[v.capacity()];
		// 	memcpy(_start, v._start, sizeof(T) * v.size());
		// 	_finish = _start + v.size();
		// 	_endofstorage = _start + v.capacity();
		// }

        // vector(const vector<T>& v)
        // {
        //     reserve(v.capacity());
        //     for (size_t i = 0; i < v.size(); ++i)
        //         push_back(v[i]);
        // }

        vector(const vector<T>& v)
        {
            vector<T> tmp(v.begin(), v.end());
            swap(tmp);
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                push_back(*first);
                ++first;
            }
        }

        void swap(vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endofstorage, v._endofstorage);
        }

        void clear()
        {
            erase(begin(), end());
        }

        vector<T>& operator=(const vector<T>& v)
        {
            if (this != &v)
            {
                reserve(v.capacity());
                for (size_t i = 0; i < v.size(); ++i)
                    _start[i] = v[i];
                _finish = _start + v.size();
            }
            return *this;
        }

        // vector<T>& operator=(vector<T> v)
        // {
        //     swap(v);
        //     return *this;
        // }

        ~vector()
        {
            if (_start)
            {
                delete[] _start;
                _start = _finish = _endofstorage = nullptr;
            }
        }

        iterator begin()
        {
            return _start;
        }

        iterator end()
        {
            return _finish;
        }

        const_iterator begin() const
        {
            return _start;
        }

        const_iterator end() const
        {
            return _finish;
        }

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

        void reserve(size_t n)
        {
            if (n > capacity())
            {
                size_t old = size();
                T* tmp = new T[n];
                if (_start)
                {
                    for (size_t i = 0; i < old; ++i)
                        tmp[i] = _start[i];
                    delete[] _start;
                }
                _start = tmp;
                _finish = _start + old;
                _endofstorage = _start + n;
            }
        }

        void resize(size_t n, T val = T())
        {
            if (n > size())
            {
                reserve(n);
                while (_finish < _start + n)
                {
                    *_finish = val;
                    ++_finish;
                }
            }
            else
                _finish = _start + n;
        }

        void push_back(const T& x)
        {
            if (_finish == _endofstorage)
            {
                size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newcapacity);
            }
            *_finish = x;
            ++_finish;
        }

        void pop_back()
        {
            assert(size() > 0);
            --_finish;
        }

        template<class InputIterator>
        void assign(InputIterator first, InputIterator last)
        {
            while (first != last)
                push_back(*first++);
        }

        void assign(int n, const T& val)
        {
            while (n--)
                push_back(val);
        }

        iterator insert(iterator pos, const T& x)
        {
            assert(pos >= _start && pos <= _finish);

            if (_finish == _endofstorage)
            {
                size_t len = pos - _start;
                reserve(capacity() == 0 ? 4 : capacity() *2);
                pos = _start + len;
            }
        }

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};
}